package com.xiaobai.sequentialControl;

import lombok.extern.slf4j.Slf4j;
/*
要求必须先打印2后打印1
 */
@Slf4j
public class sequentialControlByWaitNotify {
    static final Object obj = new Object();
    private static  boolean flag = false;
    public static void main(String[] args) {
        Thread t1 = new Thread(() -> {
            synchronized (obj){
                //如果线程2还没打印的话，那么就一直等待
              while (!flag){
                  try {
                      obj.wait();
                  } catch (InterruptedException e) {
                      throw new RuntimeException(e);
                  }
              }
              log.debug("执行....");

            }
        },"t1");
        Thread t2 = new Thread(() -> {
            synchronized (obj){
                log.debug("执行....");
                flag = true;
                obj.notify();
            }
        },"t2");
        t1.start();
        t2.start();
    }
}
